<template>
    <Dialog
      :show="dialogConfig.show"
      :title="dialogConfig.title"
      :buttons="dialogConfig.buttons"
      :showCancel="true"
      @close="dialogConfig.show = false"
        :width="600+'px'"
    >
     <div >
        <el-form
        :model="formData"
        :rules="rules"
        ref="formDataRef"
        label-width="100px"
        @submit.prevent
      >
        <el-form-item label="器材名称" prop="name" >
          <el-input :maxLength="400" placeholder="请输入器材名称" v-model="formData.name" />
        </el-form-item>

        <el-form-item
          label="器材图片"
          prop="imageUrl"
        >
          <CoverUpload
            v-model="formData.imageUrl"
            :width="200"
            :height="200"
            :type="2"
          ></CoverUpload>
        </el-form-item>

        <el-form-item label="制造商" prop="manufacturer"  >
          <el-input  v-model="formData.manufacturer" />
        </el-form-item>

        <el-form-item label="型号" prop="model"  >
          <el-input  v-model="formData.model" />
        </el-form-item>
      </el-form>
     </div>
    </Dialog>
  </template>

  <script setup>
  import { ref, reactive, getCurrentInstance, nextTick } from "vue";
  const { proxy } = getCurrentInstance();

  const api = {
    saveDeviceInfo: "/device/saveDeviceInfo",
  };


  const dialogConfig = ref({
    show: false,
    title: "新增图书",
    buttons: [
      {
        type: "primary",
        text: "确定",
        click: (e) => {
          submitForm();
        },
      },
    ],
  });

  const formData = ref({});
  const formDataRef = ref();

  const validateRePass = (rule, value, callback) => {
    if (value !== formData.value.password) {
      callback(new Error(rule.message));
    } else {
      callback();
    }
  };

  const rules = {
    name: [{ required: true, message: "请输入器材名称" }],
    imageUrl: [{ required: true, message: "请上传器材图片" }],
    manufacturer: [{ required: true, message: "请输入器材制造商" }],
    model: [{ required: true, message: "请输入器材型号" }],
  };

  const showEdit = (data) => {
    dialogConfig.value.show = true;
    nextTick(() => {
      formDataRef.value.resetFields();
      if (data.deviceId == null) {
        dialogConfig.value.title = "新增器材信息";
      } else {
        dialogConfig.value.title = "修改器材信息";
        let editData = Object.assign({}, data);
        formData.value = Object.assign({}, editData);
      }
    });
  };

  defineExpose({
    showEdit,
  });

  const emit = defineEmits(["reload"]);
  const submitForm = async () => {
    formDataRef.value.validate(async (valid) => {
      if (!valid) {
        return;
      }
      let params = {};
      Object.assign(params, formData.value);
      delete params.inventoryList
      let result = await proxy.Request({
        url: api.saveDeviceInfo,
        params,
      });
      if (!result) {
        return;
      }
      dialogConfig.value.show = false;
      proxy.Message.success("保存成功");
      emit("reload");
    });
  };
  </script>

  <style lang="scss" scoped>
  .check-span-item {
    float: left;
    margin-right: 10px;
    line-height: 20px;
  }
  </style>
